/**
 * Created by JOKER on 2017/9/5.
 */
'use strict';
(function (window, angular, undefined) {
    app.service("layerService", layerService);
    layerService.$inject = ['$http', '$q', '$rootScope'];// 注入依赖
    function layerService($http, $q, $rootScope) {
        var services = {
            popupWindow: popupWindow,
            popupModalWindow: popupModalWindow,
            popupWindowNoCallback: popupWindowNoCallback,
            popupModalWindowNoCallback: popupModalWindowNoCallback,
            alert: alert,
            waring: waring,
            closeAll: closeAll
        }
        return services;

        function popupWindow(title, content, options, yesFun, noFun, successFun) {
            var width = content.width ? (content.outerWidth()) + 'px' : 'auto',
                height = content.height ? (content.outerHeight() + 120) + 'px' : 'auto',
                defaultOptions = {
                    type: 1
                    , area: [width, height]
                    , title: title
                    , shade: 0.6
                    , closeBtn: 1
                    , maxmin: true
                    , content: content
                    , success: function (layerno, index) {
                        if (successFun) {
                            successFun(layerno);
                        }
                    }
                    , btn: ['确定', '取消']
                    , yes: function (index, layerno) {
                        $rootScope.$apply(function () {
                            if (yesFun) {
                                if (yesFun(layerno) != false) {
                                    layer.close(index);
                                }
                            } else {
                                layer.close(index);
                            }
                        });
                    }
                    , btn2: function (index, layerno) {
                        $rootScope.$apply(function () {
                            if (noFun) {
                                if (noFun(layerno) != false) {
                                    layer.close(index);
                                }
                            } else {
                                layer.close(index);
                            }
                        });
                    }
                };
            options = angular.extend(defaultOptions, options);
            return layer.open(options);
        }

        function popupModalWindow(title, content, options, ok, cancel, success) {
            options = options ? options : {};
            var defaultConfig = {
                shadeClose: false
            };
            options = angular.extend(defaultConfig, options);
            return popupWindow(title, content, options, ok, cancel, success);
        }

        function popupWindowNoCallback(title, content, options) {
            options = options ? options : {};
            var width = content.width ? (content.outerWidth()) + 'px' : 'auto';
            var height = content.height ? (content.outerHeight() + 120) + 'px' : 'auto';
            var defaultConfig = {
                type: 1,
                area: [width, height],
                title: title,
                shadeClose: false,
                content: content,
                closeBtn: 1,
                btn: ['取消']
            };
            options = angular.extend(defaultConfig, options);
            return layer.open(options);
        }

        function popupModalWindowNoCallback(title, content, options) {
            options = options ? options : {btn: []};
            return popupWindowNoCallback(title, content, options);
        }

        function openComfirm(title, content, yesFun, noFun) {
            var ops = {}
            if (angular.isFunction(yesFun)) {
                ops.btn = ['确认'];
                ops.yes = yesFun
            }
            open(title, content, ops);

        }

        function alert() {

        }

        function waring() {

        }

        function closeAll(index) {
            if (index == null) {
                layer.closeAll();
            } else {
                layer.close(index);
            }
        }
    }
})(window, window.angular);